//
//  TabelViewPresentViewController.m
//  FashionShow
//
//  Created by 张龙 on 2017/3/7.
//
//

#import "TableViewPresentViewController.h"

@interface TableViewPresentViewController ()

@property (nonatomic, readonly) UITableViewStyle style;
@property(nonatomic,strong)MBProgressHUD *progressHUD;
@property (nonatomic,assign) BOOL isPullup;//刷新方向


@end

@implementation TableViewPresentViewController
- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super init];
    if (self) {
        _style = style;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = KBACKGROUNDCOLOR;

    [self creatTableView];
    
    [self noDataView];
}
-(void)noDataView
{
    UIImageView *noDataImageView=[[UIImageView alloc] initWithFrame:CGRectMake((kScreenW-332/2)/2, (kScreenH-100-44-342/2)/2-10, 332/2, 342/2)];
    noDataImageView.backgroundColor=[UIColor clearColor];
    [self.tableView addSubview:noDataImageView];
    self.noDataImageView = noDataImageView;
    
    UILabel *noDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,noDataImageView.frame.origin.y+noDataImageView.frame.size.height-10, kScreenW-20, 40)];
    noDataLabel.backgroundColor = [UIColor clearColor];
    noDataLabel.font=[UIFont systemFontOfSize:16];
    noDataLabel.textColor = KTEXTMAINCOLOR;
    noDataLabel.numberOfLines = 0;
    noDataLabel.textAlignment= NSTextAlignmentCenter;
    [self.tableView addSubview:noDataLabel];
    self.noDataLabel = noDataLabel;
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self headerRereshing];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self footerRereshing];
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    self.pageNo=1;
    self.type=1;
    [self creatDataList];
}
- (void)footerRereshing
{
    self.type = 2;
    if (self.isMorePage)
    {
        //添加数据
        self.pageNo++;
        [self creatDataList];
    }
    else
    {
        [self.tableView.mj_footer endRefreshing];
    }
}
#pragma mark - loadData
- (void)creatDataList{
    //如果子类重写了loadData，则不再走基类的loadData
    //在这里停止刷新是为了让没有重写loadData的子类停止刷新
    [self.progressHUD hideAnimated:YES];
}
- (void)refreshCount:(NSInteger)count noDataText:(NSString *)text totalRow:(NSString *)totalRow{
    if (count <= 0 ) {
        self.noDataImageView.image = [UIImage imageNamed:@"noData.png"];
        self.noDataImageView.hidden = NO;
        self.noDataLabel.text = text;
        self.noDataLabel.hidden = NO;
    }else{
        self.noDataImageView.hidden = YES;
        self.noDataLabel.hidden = YES;
    }
    if (self.pageNo * [KREFRESHROWS intValue] < [totalRow intValue])
    {
        self.isMorePage = YES;
    }
    else
    {
        self.isMorePage = NO;
    }
    
    if (self.type == 1)
    {
        [self.tableView.mj_header endRefreshing];
    }
    else if (self.type == 2)
    {
        [self.tableView.mj_footer endRefreshing];
    }
    
}
#pragma mark - CreatTableView
- (void)creatTableView{
    if (_style) {
        _tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake( 0, 0, kScreenW, kScreenH - 64) style:_style];
    }else{
        _tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake( 0, 0, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    return view;

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

#pragma mark 网络加载指示图

-(void)showActivityWithView:(UIView *)view title:(NSString *)title
{
    
    if (!self.progressHUD) {
        MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
        progressHUD.mode=MBProgressHUDModeIndeterminate;
        progressHUD.label.text = title;
        progressHUD.alpha=0.8;
        progressHUD.label.font=[UIFont boldSystemFontOfSize:14];
        progressHUD.mj_y = 0;
        self.progressHUD = progressHUD;
        
    }
    
}

-(void)showActivityWithViewWindonWithTitle:(NSString *)title
{
    
    
    [self showActivityWithView:self.view  title:title];
}


-(void)showActivityLoadingWithView
{
    [self showActivityWithViewWindonWithTitle:@"加载中..."];
}

-(void)hiedActivityInWindon
{
    [self hideActivityInView:self.view];
}

-(void)hideActivityInView:(UIView *)view
{
    
    [self.progressHUD hideAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
